<?php
/**
 * 
 * @package    jfCachePlugin
 * @subpackage lib
 * @author     Jonas Fischer <info@jonasfischer.net>
 * @version    SVN: $Id:$
 */
class jfCache {
	
	public static $cacheTypes = array('STATIC_SOURCES' => 'STATIC_SOURCES');
 
	protected static $cacheKeysFilename = 'jfCache.js';
  protected static $cacheKeys = null;
  
  protected static function initCachKeys() {
	  if (is_null(self::$cacheKeys)) {
	  	$cacheKeysFile = sfConfig::get('sf_cache_dir').'/'.self::$cacheKeysFilename;
	  	if (file_exists($cacheKeysFile)) {
	   	  self::$cacheKeys = json_decode(trim(@file_get_contents($cacheKeysFile)), true);
	  	}
	  	else {
	  		$cacheKeyValue = time();
	  		$cacheKeys= array();
	  		foreach (self::$cacheTypes as $cacheKeyName) {
	  			$cacheKeys[$cacheKeyName] = $cacheKeyValue;
	  		}
	  		self::$cacheKeys = $cacheKeys;
	  		self::storeCacheKeys();
	  	}
	  }
  }
  
  protected static function storeCacheKeys() {
	 	$cacheKeysFile = sfConfig::get('sf_cache_dir').'/'.self::$cacheKeysFilename;
   	file_put_contents($cacheKeysFile, json_encode(self::$cacheKeys));
  }
  
  public static function getCacheKeyValue($type, $default = null) {
	  self::initCachKeys();
    
	  $cacheKeyValue = null;
	  if (is_array(self::$cacheKeys) && array_key_exists($type, self::$cacheKeys)) {
	  	$cacheKeyValue = self::$cacheKeys[$type];
	  }
	  else if ($default) {
	  	self::setCacheKeyValue($type, $default);
	  	return self::getCacheKeyValue($type);
	  }

	  return $cacheKeyValue;
  }
  
  
  public static function setCacheKeyValue($type, $value) {
  	self::initCachKeys();
	  
    self::$cacheKeys[$type] = $value;
    self::storeCacheKeys();
  }
  

}
